package com.summer_administration.controller;

import com.summer_administration.pojo.Income;
import com.summer_administration.pojo.PageBean;
import com.summer_administration.pojo.Result;
import com.summer_administration.service.IncomeService;
import jakarta.validation.constraints.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.github.pagehelper.util.MetaObjectUtil.method;

@RestController
@RequestMapping("/income")
@Validated
public class IncomeController {
    @Autowired
    private IncomeService incomeService;

    @PostMapping
    public Result add(@RequestBody @Validated Income income) {
        incomeService.add(income);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody @Validated Income income) {
        incomeService.update(income);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Income>> list(
            Integer pageNum,
            Integer pageSize,
                @RequestParam(required=false) String method
    ){
        PageBean<Income> pb = incomeService.list(pageNum,pageSize,method);
        return Result.success(pb);
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        incomeService.delete(id);
        return Result.success();
    }


    //根据小组名获取小组id
    @GetMapping("/getIdByGroupName")
    public Result<String> getGroupIdByGroupName(@RequestParam String groupName) {
        String groupId = incomeService.getGroupIdByGroupName(groupName);
        return Result.success(groupId);
    }
}
